//
// Created by evocrab on 10/25/17.
//

#include <vector>
#include <map>
#include "LeetcodeSolutions.h"
//#include "LeetcodeDataStruct.h"

using namespace std;

/**
 * leet code 1
 */
//my solution
vector<int> LeetcodeSolutions::twoSum(vector<int> &nums, int target) {
    for (int counter = 0; counter < nums.size(); counter++) {
        for (int secCounter = counter + 1; secCounter < nums.size(); secCounter++) {
            if ((nums[counter] + nums[secCounter]) == target) {
                return {counter, secCounter};
            }
        }
    }
}

//winner's solution
vector<int> LeetcodeSolutions::twoSumTwo(vector<int> &nums, int target) {
    map<int, int> my_map;
    map<int, int>::iterator it;
    for (int counter = 0; counter < nums.size(); counter++) {
        int exceptResult = target - nums[counter];
        it = my_map.find(exceptResult);
        if (it != my_map.end()) {
            int key = it->second;
            return {counter, key};
        }
        my_map[nums[counter]] = counter;
    }
}

ListNode *LeetcodeSolutions::addTwoNumbers(ListNode *l1, ListNode *l2) {
    ListNode *ll1 = l1;
    ListNode *ll2 = l2;
    ListNode *l3 = NULL;
    ListNode *currentNode = NULL;
    bool flag = false;

    while (NULL != ll1 || NULL != ll2) {
        int val = 0;
        int flagCounter = 0;
        if (flag) {
            flagCounter = 1;
        }
        val = (ll1 != NULL ? (*ll1).val : 0) + (ll2 != NULL ? (*ll2).val : 0) + flagCounter;
        flag = val >= 10;
        val = val % 10;
        if (NULL == currentNode) {
            currentNode = new ListNode(val);
            l3 = currentNode;
        } else {
            (*currentNode).next = new ListNode(val);
            currentNode = (*currentNode).next;
        }

        if (NULL != ll1) {
            ll1 = (*ll1).next;
        }
        if (NULL != ll2) {
            ll2 = (*ll2).next;
        }
    }
    if (flag) {
        (*currentNode).next = new ListNode(1);
    }

    return l3;
}

int LeetcodeSolutions::lengthOfLongstSubstring(std::string s) {
    

    return 0;
}




